package com.justgame.project.service.online.feign;

import com.justgame.project.cloud.common.constant.Regex;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.service.api.dto.OnlineUserInfoDTO;
import com.justgame.project.service.api.entity.OnlineUserInfo;
import com.justgame.project.service.api.feign.IOnlineUserClient;
import com.justgame.project.service.online.mapper.OnlineUserInfoMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouWeiBo
 * @create 2022/4/12
 */
@RestController
@AllArgsConstructor
public class OnlineUserClient implements IOnlineUserClient {

    private final OnlineUserInfoMapper onlineUserInfoMapper;

    @Override
    @PostMapping(SAVE_ONLINE_USER_URL)
    public Boolean saveOnlineUser(@RequestBody OnlineUserInfoDTO dto) {
        return onlineUserInfoMapper.saveOnlineUser(dto);
    }

    @Override
    @PutMapping(UPDATE_ONLINE_USER_URL)
    public Boolean updateById(@RequestBody OnlineUserInfoDTO dto) {
        return onlineUserInfoMapper.updateById(dto) >= 1;
    }

    @Override
    @GetMapping(GET_ONLINE_USER_INFO_URL)
    public OnlineUserInfo getOnlineInfo(@RequestParam("id") String id) {
        return onlineUserInfoMapper.getOnlineInfo(id);
    }

    @Override
    @PostMapping(UPDATE_ONLINE_USER_IMAGE_URL)
    public Boolean updateUserImage(@RequestBody OnlineUserInfoDTO dto) {
        if (    !ObjUtil.checkId(dto) ||
                !ObjUtil.checkId(dto.getOnlineId()) ||
                StringUtils.isBlank(dto.getImages()) ||
                !Regex.isUrl(dto.getImages())
        ){
            return false;
        }
        OnlineUserInfoDTO newDto = new OnlineUserInfoDTO();
        newDto.setUserId(dto.getUserId());
        newDto.setOnlineId(dto.getOnlineId());
        newDto.setImages(dto.getImages());
        return onlineUserInfoMapper.updateById(newDto) > 0;
    }
}
